var Comment = require('../model/comment')

exports.save = function(req, res) {
    var commentObj = req.body.comment;
    var blogId = commentObj.blog;
    console.log(commentObj)
    if (commentObj.cid) {
        Comment.findOneByAttr({
            _id: commentObj.cid
        }, function(err, comment) {
            var reply = {
                from: commentObj.from,
                to: commentObj.tid,
                content: commentObj.content
            }
            comment.reply.push(reply);
            comment.save(function(err, comment) {
                if (err) {
                    console.log(err);
                } else {
                    res.redirect('/single/' + blogId);
                }
            });
        });
    } else {
        var comment = new Comment(commentObj);
        comment.save(function(err, comment) {
            if (err) {
                console.log(err);
            } else {
                res.redirect('/single/' + blogId);
            }
        });
    }



}
